import { ref } from 'vue'

interface RequestOptions {
  url: string
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
  data?: any
  headers?: Record<string, string>
}

class HttpService {
  private baseUrl: string

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl
  }

  async request(options: RequestOptions) {
    const { url, method = 'GET', data, headers } = options
    const fullUrl = this.baseUrl + url

    try {
      const response = await uni.request({
        url: fullUrl,
        method,
        data,
        header: headers
      })

      if (response.statusCode >= 200 && response.statusCode < 300) {
        return response.data
      } else {
        throw new Error(`Request failed with status code: ${response.statusCode}`)
      }
    } catch (error) {
      throw new Error(`Network error: ${error.message}`)
    }
  }
}

export default HttpService